package ext.tianma.cascade.util;

import java.io.File;
import java.util.LinkedHashMap;

import org.apache.poi.ss.usermodel.Workbook;

import ext.tianma.cascade.bean.CascadeBean;
import ext.tianma.cascade.util.CascadeHelper.CascadeSheetName;
import wt.util.WTException;

public class CascadeCache extends LinkedHashMap<CascadeSheetName, CascadeBean> {
	
	private static final long serialVersionUID = -4909209855206688123L;

	private static CascadeCache cache;
	
	private long configModifyTime = 0;
	private long tempTime = 0;
	
	private CascadeCache() {
		super();
	}
	
	public static CascadeCache getInstance() {
		if(cache == null) {
			cache = new CascadeCache();
		}
		return cache;
	}
	
	private void initCache() {
		clear();
		Workbook workbook = null;
		try {
			workbook = CascadeHelper.getCascadeWorkbook();
		} catch (WTException e) {
			e.printStackTrace();
		}
		if(workbook != null) {
			for (CascadeSheetName sheetName : CascadeSheetName.class.getEnumConstants()) {
				put(sheetName, CascadeHelper.getCascade(workbook, sheetName));
				System.out.println("====>>>");
			}
		}
		configModifyTime = tempTime;
	}
	
	/**
	 * 级联配置文件是否已修改
	 * @return
	 */
	private boolean isModify() {
		File file = new File(CascadeHelper.CASCADE_PATH);
		tempTime = file.lastModified();
		System.out.println("tempTime====>>>"+tempTime);
		return configModifyTime != tempTime;
	}
	
	public CascadeBean get(Object key) {
		System.out.println("key===>>"+key+"isModify()==>>+"+isModify());
//		if(isModify()) {
			System.out.println("==11==>>");
			initCache();
//		}
		return super.get(key);
	}

	public void clear() {
		configModifyTime = 0;
		super.clear();
	}
}
